package com.community.charge.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;
import com.community.charge.domain.Bill;
import com.community.charge.service.IBillService;
import com.community.common.utils.poi.ExcelUtil;
import com.community.common.core.page.TableDataInfo;

/**
 * 账单管理Controller
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/charge/billa")
public class BillController extends BaseController
{
    @Autowired
    private IBillService billService;


    /**
     * 账单状态统计
     */
    @GetMapping("/groupByStatus")
    public AjaxResult groupByStatus(){
        return success(billService.groupByStatus());
    }
    /**
     * 统计待缴费账单
     */
    @GetMapping("/count")
    public AjaxResult count(){
        return success(billService.count());
    }


    /**
     * 查询账单管理列表
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bill bill)
    {
        startPage();
        List<Bill> list = billService.selectBillList(bill);
        return getDataTable(list);
    }

    /**
     * 导出账单管理列表
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:export')")
    @Log(title = "账单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bill bill)
    {
        List<Bill> list = billService.selectBillList(bill);
        ExcelUtil<Bill> util = new ExcelUtil<Bill>(Bill.class);
        util.exportExcel(response, list, "账单管理数据");
    }

    /**
     * 获取账单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return success(billService.selectBillByBillId(billId));
    }

    /**
     * 新增账单管理
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:add')")
    @Log(title = "账单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bill bill)
    {
        return toAjax(billService.insertBill(bill));
    }

    /**
     * 修改账单管理
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:edit')")
    @Log(title = "账单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bill bill)
    {
        return toAjax(billService.updateBill(bill));
    }

    /**
     * 删除账单管理
     */
    @PreAuthorize("@ss.hasPermi('charge:billa:remove')")
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds) {
        //查询要删除那条数据的支付状态
        Integer i = billService.getIdByStatus(billIds);
        if (i!=0){
            return error("该账单未支付，删除失败！！！");
        }
        return toAjax(billService.deleteBillByBillIds(billIds));
    }


}
